package cn.ctodb.notebook.wxapp.service.impl;

import cn.ctodb.notebook.wxapp.service.PicService;
import cn.ctodb.notebook.wxapp.domain.Pic;
import cn.ctodb.notebook.wxapp.repository.PicRepository;
import cn.ctodb.notebook.wxapp.service.dto.PicDTO;
import cn.ctodb.notebook.wxapp.service.mapper.PicMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Service Implementation for managing Pic.
 */
@Service
@Transactional
public class PicServiceImpl implements PicService {

    private final Logger log = LoggerFactory.getLogger(PicServiceImpl.class);

    private final PicRepository picRepository;

    private final PicMapper picMapper;

    public PicServiceImpl(PicRepository picRepository, PicMapper picMapper) {
        this.picRepository = picRepository;
        this.picMapper = picMapper;
    }

    /**
     * Save a pic.
     *
     * @param picDTO the entity to save
     * @return the persisted entity
     */
    @Override
    public PicDTO save(PicDTO picDTO) {
        log.debug("Request to save Pic : {}", picDTO);
        Pic pic = picMapper.toEntity(picDTO);
        pic = picRepository.save(pic);
        return picMapper.toDto(pic);
    }

    /**
     * Get all the pics.
     *
     * @return the list of entities
     */
    @Override
    @Transactional(readOnly = true)
    public List<PicDTO> findAll() {
        log.debug("Request to get all Pics");
        return picRepository.findAll().stream()
            .map(picMapper::toDto)
            .collect(Collectors.toCollection(LinkedList::new));
    }

    /**
     * Get one pic by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Override
    @Transactional(readOnly = true)
    public PicDTO findOne(Long id) {
        log.debug("Request to get Pic : {}", id);
        Pic pic = picRepository.findOne(id);
        return picMapper.toDto(pic);
    }

    /**
     * Delete the pic by id.
     *
     * @param id the id of the entity
     */
    @Override
    public void delete(Long id) {
        log.debug("Request to delete Pic : {}", id);
        picRepository.delete(id);
    }
}
